const nodemailer = require('nodemailer');
require('dotenv').config();

// 创建邮件传输对象
const transporter = nodemailer.createTransport({
  host: process.env.MAIL_HOST,
  port: process.env.MAIL_PORT,
  secure: process.env.MAIL_PORT === '465', // true for 465, false for other ports
  auth: {
    user: process.env.MAIL_USER,
    pass: process.env.MAIL_PASS
  }
});

// 验证邮件配置
const verifyConfig = async () => {
  try {
    await transporter.verify();
    console.log('邮件服务配置验证成功');
    return true;
  } catch (error) {
    console.error('邮件服务配置验证失败:', error);
    return false;
  }
};

// 发送验证码邮件
const sendVerificationCode = async (to, code, type = 'register') => {
  const subject = type === 'register' ? '注册验证码' : '重置密码验证码';
  const template = `
    <div style="max-width: 600px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif;">
      <h2 style="color: #409EFF; text-align: center;">标王资料网</h2>
      <div style="background-color: #f5f7fa; padding: 20px; border-radius: 5px;">
        <h3 style="margin-top: 0;">您的${subject}</h3>
        <p>您好，</p>
        <p>您的验证码是：</p>
        <div style="background-color: #fff; padding: 15px; text-align: center; font-size: 24px; font-weight: bold; color: #409EFF; border-radius: 5px; margin: 20px 0;">
          ${code}
        </div>
        <p>验证码有效期为10分钟，请尽快使用。</p>
        <p>如果这不是您的操作，请忽略此邮件。</p>
      </div>
      <div style="text-align: center; margin-top: 20px; color: #909399; font-size: 12px;">
        <p>此邮件由系统自动发送，请勿回复</p>
        <p>© 2024 标王资料网 版权所有</p>
      </div>
    </div>
  `;

  try {
    await transporter.sendMail({
      from: process.env.MAIL_FROM,
      to,
      subject: `【标王资料网】${subject}`,
      html: template
    });
    return true;
  } catch (error) {
    console.error('发送验证码邮件失败:', error);
    return false;
  }
};

// 发送注册成功邮件
const sendWelcomeEmail = async (to) => {
  const template = `
    <div style="max-width: 600px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif;">
      <h2 style="color: #409EFF; text-align: center;">标王资料网</h2>
      <div style="background-color: #f5f7fa; padding: 20px; border-radius: 5px;">
        <h3 style="margin-top: 0;">注册成功</h3>
        <p>尊敬的用户，</p>
        <p>欢迎加入标王资料网！您的账号已经成功激活。</p>
        <p>现在您可以：</p>
        <ul style="color: #606266;">
          <li>浏览和下载各类投标资料</li>
          <li>充值虚拟币购买资料</li>
          <li>享受专业的投标服务</li>
        </ul>
        <div style="text-align: center; margin: 20px 0;">
          <a href="${process.env.FRONTEND_URL}" 
             style="background-color: #409EFF; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">
            立即访问
          </a>
        </div>
      </div>
      <div style="text-align: center; margin-top: 20px; color: #909399; font-size: 12px;">
        <p>此邮件由系统自动发送，请勿回复</p>
        <p>© 2024 标王资料网 版权所有</p>
      </div>
    </div>
  `;

  try {
    await transporter.sendMail({
      from: process.env.MAIL_FROM,
      to,
      subject: '【标王资料网】注册成功',
      html: template
    });
    return true;
  } catch (error) {
    console.error('发送注册成功邮件失败:', error);
    return false;
  }
};

// 发送密码重置成功邮件
const sendPasswordResetSuccessEmail = async (to) => {
  const template = `
    <div style="max-width: 600px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif;">
      <h2 style="color: #409EFF; text-align: center;">标王资料网</h2>
      <div style="background-color: #f5f7fa; padding: 20px; border-radius: 5px;">
        <h3 style="margin-top: 0;">密码重置成功</h3>
        <p>尊敬的用户，</p>
        <p>您的密码已经成功重置。</p>
        <p>如果这不是您的操作，请立即：</p>
        <ol style="color: #606266;">
          <li>登录您的账号</li>
          <li>修改密码</li>
          <li>联系客服报告可疑活动</li>
        </ol>
        <div style="text-align: center; margin: 20px 0;">
          <a href="${process.env.FRONTEND_URL}/login" 
             style="background-color: #409EFF; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">
            立即登录
          </a>
        </div>
      </div>
      <div style="text-align: center; margin-top: 20px; color: #909399; font-size: 12px;">
        <p>此邮件由系统自动发送，请勿回复</p>
        <p>© 2024 标王资料网 版权所有</p>
      </div>
    </div>
  `;

  try {
    await transporter.sendMail({
      from: process.env.MAIL_FROM,
      to,
      subject: '【标王资料网】密码重置成功',
      html: template
    });
    return true;
  } catch (error) {
    console.error('发送密码重置成功邮件失败:', error);
    return false;
  }
};

module.exports = {
  verifyConfig,
  sendVerificationCode,
  sendWelcomeEmail,
  sendPasswordResetSuccessEmail
};